package com.android.sms_manager.data;



import java.util.List;
import java.util.Map;

import com.android.sms_manager.R;
import com.android.sms_manager.service.QueryService;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

public class MySmsAdapter implements ListAdapter {

	private List<Map<String, Object>> data;
	private Context context = null;
	private int thread_id;
	private String uri = null;
	private int protocol;
	private int id = 0;
	//private int id;
	private LayoutInflater inflater = null;
	
	public MySmsAdapter(Context context, int thread_id, int protocol,
			String uri, int id) {
		this.context = context;
		this.thread_id = thread_id;
		this.uri = uri;
		this.protocol = protocol;
		this.id = id;
		inflater = LayoutInflater.from(context);
		loadData();
	}

	public void loadData() {
		data = null;
		data = QueryService.getAllSmsByThread_id(context, uri, thread_id,
				protocol);		
	}

	public int getposition(int ID){
		int po = 0;
		if(data == null)
			return 0;
		else{
			for(int i = 0;i<data.size();i++){
				Map<?, ?> map = data.get(i);
				if(ID == (Integer)(map.get(MyData.KEY_ID))){
					po = i;
					break;
				}
			}
			
			return po;
		}
	}
	public int getCount() {		
		if (data == null)
			return 0;
		else
			return data.size();
	}
	
	/**
	 * delete a sms
	 * Context context,int _id,int type,int protocol
	 * @param position
	 */
	public void deleteItem(int position,int type){
		int id = (Integer)data.get(position).get(MyData.KEY_ID);
		QueryService.deleteById(context, id, type, protocol);
		Log.i("delete", "delete id = "+id);
	}

	private class SmsViewHolder {
		public SmsViewHolder() {
		}

		TextView date = null;	
		TextView body = null;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		if (data == null)
			return null;
		else
			return getSmsView(position, convertView, parent);

	}	
	/**
	 * get the view of sms
	 * @param position
	 * @param convertView
	 * @param parent
	 * @return View
	 */
	private View getSmsView(int position, View convertView, ViewGroup parent){
		if (data == null)
			return null;

		SmsViewHolder holder = null;
		if (convertView == null) {
//			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = (LinearLayout) inflater.inflate(R.layout.smsitem,
					parent, false);
			holder = new SmsViewHolder();

			holder.date = (TextView) convertView.findViewById(R.id.smsdate);
			
			holder.body = (TextView) convertView.findViewById(R.id.smsbody);
			
			convertView.setTag(holder);
		} else {
			convertView = (LinearLayout) convertView;
			holder = (SmsViewHolder) convertView.getTag();
		}

		Map<?, ?> map = data.get(position);
		
			
		// set view disappear by content
//**********************************************		
		if(map.get(MyData.KEY_TYPE).toString().equals(""+MyData.TYPE_INBOX))
		
		{
			holder.date.setGravity(Gravity.LEFT);
			holder.body.setGravity(Gravity.LEFT);
		}
		else 
		{
			holder.date.setGravity(Gravity.RIGHT);
			holder.body.setGravity(Gravity.RIGHT);
		}
//*****************************************************************	
		if(!(map.get(MyData.KEY_ID).toString().equals(""+id)))
		//	if (position != this.selectItem)
		{			
			//Log.i("color", id + ","+map.get(MyData.KEY_ID).toString());
			convertView.setBackgroundResource(R.color.upbackground);}
		else{
			convertView.setBackgroundResource(R.color.light_gray);
		}
//*****************************************************		
		if(map.get(MyData.KEY_TYPE).toString().equals(""+MyData.TYPE_FAILED)){
			holder.date.setTextColor(Color.RED);
			String s = this.context.getString(R.string.sendfail);
			holder.date.setText(s+map.get(MyData.KEY_DATE).toString());
		}
		else{
			holder.date.setTextColor(Color.GRAY);
			holder.date.setText(map.get(MyData.KEY_DATE).toString());
		}
			
//***************************************************				
		holder.body.setText(map.get(MyData.KEY_BODY).toString());
		
		return convertView;
	}
	
	public void setSelectItem(int i){
	}
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int getItemViewType(int arg0) {
		// TODO Auto-generated method stub
		return 1;
	}
	
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 1;
	}

	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}

	public void registerDataSetObserver(DataSetObserver arg0) {
		// TODO Auto-generated method stub

	}

	public void unregisterDataSetObserver(DataSetObserver arg0) {
		// TODO Auto-generated method stub

	}

	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean isEnabled(int arg0) {
		// TODO Auto-generated method stub
		return true;
	}

}
